package com.homedo.odin.solution.service.bizservice.personal;

import com.github.pagehelper.PageInfo;
import com.homedo.core.common.base.dto.Meta;
import com.homedo.core.common.exception.BizException;
import com.homedo.core.common.util.BeanUtils;
import com.homedo.core.common.util.ObjectConverter;
import com.homedo.odin.solution.contract.pc.GetBountyReplyListResp;
import com.homedo.odin.solution.contract.pc.GetMyBountyReplyListReq;
import com.homedo.odin.solution.contract.pc.GetMyBountyReplyListResp;
import com.homedo.odin.solution.contract.pc.InsertBountyReplyReq;
import com.homedo.odin.solution.enumcode.TemplateCodeEnum;
import com.homedo.odin.solution.enumcode.bounty.BountyAdoptEnum;
import com.homedo.odin.solution.model.dto.PageBean;
import com.homedo.odin.solution.model.entity.Bounty;
import com.homedo.odin.solution.model.entity.BountyReply;
import com.homedo.odin.solution.model.entity.BountyReplyBean;
import com.homedo.odin.solution.model.entity.UserBasic;
import com.homedo.odin.solution.service.BountyReplyServiceImpl;
import com.homedo.odin.solution.service.BountyServiceImpl;
import com.homedo.odin.solution.service.BountyTotalServiceImpl;
import com.homedo.odin.solution.service.UserBasicServiceImpl;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author: donglibing
 * @date: 2018/3/7 18:58
 * @Description: 悬赏回复业务处理
 */
@Service
public class BountyReplyBizService {

    @Autowired
    BountyReplyServiceImpl impl;

    @Autowired
    BountyTotalServiceImpl bountyTotalService;

    @Autowired
    BountyServiceImpl bountyBizService;

    @Autowired
    UserBasicServiceImpl userBasicService;

    @Autowired
    CommonBizService commonBizService;

    /**
     * 获取悬赏回复列表
     */
    public GetBountyReplyListResp getBountyReplyList(int pageIndex, int pageSize, int bountyId) {
        GetBountyReplyListResp resp = new GetBountyReplyListResp();
        PageBean pageBean = new PageBean(pageIndex, pageSize);
        PageInfo<BountyReplyBean> bountyReplyList = impl.getBountyReplyList(pageBean, bountyId);
        resp = BeanUtils.transfrom(GetBountyReplyListResp.class, bountyReplyList);
        return resp;
    }

    /**
     * 获取我的悬赏回复列表
     */
    public GetMyBountyReplyListResp getMyBountyReplyList(GetMyBountyReplyListReq req) {
        GetMyBountyReplyListResp resp = new GetMyBountyReplyListResp();
        PageBean pageBean = new PageBean(req.getPageNum(), req.getPageSize());
        if (req.getEndTime() == null || req.getStartTime() == null) {
            req.setEndTime(Long.valueOf(0));
            req.setStartTime(Long.valueOf(0));
        }
        PageInfo<BountyReplyBean> bountyReplyList = impl
                .getMyBountyReplyList(pageBean, req.getUserId(), req.getStatus(), req.getStartTime(), req.getEndTime());
        resp = BeanUtils.transfrom(GetMyBountyReplyListResp.class, bountyReplyList);
        return resp;
    }

    /**
     * 插入悬赏回复
     */
    @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
    public void insert(InsertBountyReplyReq req) {

        //1.数据校验
        if (req.getBountyId() <= 0 || req.getReplyContent() == null || req.getUserId() <= 0) {
            throw new BizException(Meta.CODE_451.getCode(), "传入参数校验失败");
        }
        //判断是否有该用户
        UserBasic userBasic = commonBizService.validationUser(req.getUserId());

        BountyReply bountyReplyBean = new BountyReply();
        ObjectConverter.copyProperties(req, bountyReplyBean);
        bountyReplyBean.setCreatedBy(req.getUserId());
        bountyReplyBean.setModifiedBy(req.getUserId());
        bountyReplyBean.setUserName(userBasic.getUserName());
        bountyReplyBean.setIsAdopt(BountyAdoptEnum.NOT_ADOPT.getDbValue());
        //2.插入悬赏回复
        boolean result1 = impl.insert(bountyReplyBean);
        if (!result1) {
            throw new BizException(Meta.CODE_500.getCode(), "插入悬赏回复失败");
        }
        //3.更新回复数量
        Bounty bounty = bountyBizService.checkBounty(bountyReplyBean.getBountyId());
        if (bounty == null) {
            throw new BizException(Meta.CODE_500.getCode(), "不存在该悬赏");
        }
        int count = bounty.getReplyCount() + 1;
        bounty.setReplyCount(count);
        boolean result = bountyBizService.updateById(bounty);
        if (!result) {
            throw new BizException(Meta.CODE_500.getCode(), "更新悬赏答复数失败");
        }
        //4.发送信息
        Map<String, String> map3 = new HashMap<>();
        map3.put("@@userName", bounty.getUserName());
        map3.put("@@bounty", bounty.getTitle());
        map3.put("@@bizId", bounty.getId().toString());
        commonBizService.sentMessage(map3, bounty.getUserId(), 0, TemplateCodeEnum.bounty_reply.toString());
    }


}
